﻿using System;
using System.Runtime.Serialization;

namespace CI.Organism
{
    /// <summary>
    /// Struct, External Synaptic IO, eg a bumber, wheel etc
    /// </summary>
    [DataContract(IsReference = true)]
    public class SynapseIO : ISynapticConnection
    {
        /// <summary>Binding name eg. IROBOTLEFTREARWHEELFORWARD</summary>
        [DataMember]
        public string BindingName { get; private set; }

        /// <summary>Short name eg. left front wheel forward</summary>
        [DataMember]
        public string ShortName { get; private set; }

        /// <summary>description eg. This bound object is the Front wheel drive system</summary>
        [DataMember]
        public string Description { get; private set; }

        /// <summary>SynapseIO GUID used to uniquely identify it</summary>
        [DataMember]
        public Guid Guid { get; private set; }

        /// <summary>
        /// The currrent value of the Synaptic IO
        /// </summary>
        public Int64 CurrentDisplayValue
        {
            get
            {
                return _currentDisplayValue;
            }
            private set
            {
                _currentValueHBReset = lastNHBReset;
                _currentDisplayValue = value;
            }
        }
        private Int64 _currentDisplayValue;
        private Int64 _currentValueHBReset;

        private Int64 lastNHBReset = 0;

        /// <summary>
        /// Notify any attachments that the activation has fired with a value
        /// </summary>
        public event Action<long> ActivationEvent;

        /// <summary>
        /// Initial struct with value
        /// </summary>
        /// <param name="bindingName">binding name</param>
        /// <param name="shortName">short name</param>
        /// <param name="description">description</param>
        /// <param name="guid"></param>
        public SynapseIO(string bindingName, string shortName, string description, Guid guid)
        {
            BindingName = bindingName;
            ShortName = shortName;
            Description = description;
            Guid = guid;
        }

        /// <summary>
        /// To String implmentation of the Synapse IO
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return BindingName;
        }

        /// <summary>
        /// Fire the synapse, notify it that a potential has arrived and the value
        /// </summary>
        public void FireInput(long inputValue)
        {
            CurrentDisplayValue = inputValue;

            if (ActivationEvent != null)
                ActivationEvent(inputValue);
        }

        /// <summary>
        /// reset the current value if the neural hb has incremented
        /// </summary>
        public void Reset(Int64 neuralHB)
        {
            if (_currentValueHBReset < neuralHB)
            {
                _currentDisplayValue = 0;
            }

            lastNHBReset = neuralHB + 1;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            ActivationEvent = null;
        }

        public override bool Equals(object synapseIO)
        {
            if (ReferenceEquals(null, synapseIO)) return false;
            if (ReferenceEquals(this, synapseIO)) return true;
            if (synapseIO.GetType() != typeof(SynapseIO)) return false;
            return ((SynapseIO)synapseIO).Guid.Equals(Guid);
        }

        public override int GetHashCode()
        {
            return Guid.GetHashCode();
        }
    }
}